function comprova() {
    estat = true
    document.getElementById("loading").innerHTML = "";

    /* Els mirem u per u, es podia fer amb un array,... */
    /* Primer mirem si estan buits els elements del formulari */

    /* Nom */
    if(document.getElementById('Nom').value == "") {						
        actual = document.getElementById('Nom');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" * El nom es obligatori");
        miSpan.appendChild(miText);
        document.getElementById('label'+document.getElementById('Nom').id).innerHTML = "";
        document.getElementById('label'+document.getElementById('Nom').id).appendChild(miSpan);
        document.getElementById('Nom').style.background = "red";
        estat = false;					
    }
    else {
        document.getElementById('label'+document.getElementById('Nom').id).innerHTML = document.getElementById('Nom').id +  " :";
        document.getElementById('Nom').style.background = "AQUAMARINE";
    }

    /* PrimerCognom */
    if(document.getElementById('PrimerCognom').value == "") {						
        actual = document.getElementById('PrimerCognom');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" * El primer cognom es obligatori");
        miSpan.appendChild(miText);
        document.getElementById('label'+document.getElementById('PrimerCognom').id).innerHTML = "";
        document.getElementById('label'+document.getElementById('PrimerCognom').id).appendChild(miSpan);
        document.getElementById('PrimerCognom').style.background = "red";
        estat = false;					
    }
    else {
        document.getElementById('label'+document.getElementById('PrimerCognom').id).innerHTML = "Primer cognom :";
        document.getElementById('PrimerCognom').style.background = "AQUAMARINE";
    }

    /* SegonCognom */
    if(document.getElementById('SegonCognom').value == "") {						
        actual = document.getElementById('SegonCognom');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" * El segon cognom es obligatori");
        miSpan.appendChild(miText);
        document.getElementById('label'+document.getElementById('SegonCognom').id).innerHTML = "";
        document.getElementById('label'+document.getElementById('SegonCognom').id).appendChild(miSpan);
        document.getElementById('SegonCognom').style.background = "red";
        estat = false;
    }
    else {
        document.getElementById('label'+document.getElementById('SegonCognom').id).innerHTML = "Segon cognom :";
        document.getElementById('SegonCognom').style.background = "AQUAMARINE";
    }

    /* E_mail */
    if(document.getElementById('E-mail').value == "") {						
        actual = document.getElementById('E-mail');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" * El E-mail es obligatori");
        miSpan.appendChild(miText);
        document.getElementById('label'+document.getElementById('E-mail').id).innerHTML = "";
        document.getElementById('label'+document.getElementById('E-mail').id).appendChild(miSpan);
        document.getElementById('E-mail').style.background = "red";
        estat = false;
    }
    else {
        document.getElementById('label'+document.getElementById('E-mail').id).innerHTML = document.getElementById('E-mail').id + ":";
        document.getElementById('E-mail').style.background = "AQUAMARINE";
        //mirem format mail
        if(estat) estat = checkmail();
    }

    /* Password */
    if(document.getElementById('Password').value == "") {						
        actual = document.getElementById('Password');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" * El password es obligatori");
        miSpan.appendChild(miText);
        document.getElementById('label'+document.getElementById('Password').id).innerHTML = "";
        document.getElementById('label'+document.getElementById('Password').id).appendChild(miSpan);
        document.getElementById('Password').style.background = "red";
        estat = false;					
    }
    else {
        document.getElementById('label'+document.getElementById('Password').id).innerHTML = document.getElementById('Password').id + " :";
        document.getElementById('Password').style.background = "AQUAMARINE";
    }

    /* RePassword */
    if(document.getElementById('RePassword').value == "") {						
        actual = document.getElementById('RePassword');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" * Aquest camp es obligatori");
        miSpan.appendChild(miText);
        document.getElementById('label'+document.getElementById('RePassword').id).innerHTML = "";
        document.getElementById('label'+document.getElementById('RePassword').id).appendChild(miSpan);
        document.getElementById('RePassword').style.background = "red";
        estat = false;
    }
    else {
        document.getElementById('label'+document.getElementById('RePassword').id).innerHTML = "Repeteix el password :";
        document.getElementById('RePassword').style.background = "AQUAMARINE";
    }

    /* DataNaixament */
    if(document.getElementById('DataNaixament').value == "") {						
        actual = document.getElementById('DataNaixament');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" * La data naixament es obligatoria");
        miSpan.appendChild(miText);
        document.getElementById('labelData Naixament').innerHTML = "";
        document.getElementById('labelData Naixament').appendChild(miSpan);
        document.getElementById('DataNaixament').style.background = "red";
        estat = false;
    }
    else {
        document.getElementById('labelData Naixament').innerHTML = "Data naixament: ";
        document.getElementById('DataNaixament').style.background = "AQUAMARINE";
        //mirem format data naixament
        if(estat) estat = checkdata();
    }
    
    if(estat) estat = comparaPasswords();
    
    if(document.getElementById('Casella').checked == false && estat){
        alert("Has d'acceptar els termes de contactes");
        document.getElementById('labelCasella').style.background = "red";
        return false;
    }

    finals = "Les teves dades són:\nNom: " + document.getElementById('Nom').value +
             "\nPrimer cognom: " + document.getElementById('PrimerCognom').value +
             "\nSegon cognom: " + document.getElementById('SegonCognom').value +
             "\nData Naixament: " + document.getElementById('DataNaixament').value + 
             "\nCom ens ha trobat?: " + document.getElementById('Com ens ha trobat?').value;
    if(estat) {
        if (confirm(finals)) estat = true;
        else estat=false;
    }
    
    if(estat) checkUserAjax(document.getElementById('miform'));

    return false;
}

function comparaPasswords() {
    devuelve = true;
    if(document.getElementById('RePassword').value != document.getElementById('Password').value ){		
        actual = document.getElementById('RePassword');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" * El passwords no coincideixen");
        miSpan.appendChild(miText);
        document.getElementById('label'+document.getElementById('RePassword').id).innerHTML = "";
        document.getElementById('label'+document.getElementById('RePassword').id).appendChild(miSpan);
        document.getElementById('RePassword').style.background = "red";
        devuelve = false;
    }
    
    return devuelve;	
}

function checkmail() {
    retorna = true;			
    var expresio = new RegExp(/^[a-zA-Z0-9!#$%&?^\/_'{}|~=*+-]+(\.[a-zA-Z0-9!#$%&?^\/_'{}|~=*+-]+)*@([a-zA-Z0-9\-])+(\.([a-zA-Z0-9\-])+)+$/);
    mail = document.getElementById('E-mail').value;
    if(expresio.test(mail)){
        document.getElementById('labelE-mail').innerHTML = "E-mail";
        document.getElementById('E-mail').style.background = "AQUAMARINE";
    }
    else{
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" * E-mail introduït es incorrecte");
        miSpan.appendChild(miText);
        document.getElementById('labelE-mail').innerHTML = "";
        document.getElementById('labelE-mail').appendChild(miSpan);
        document.getElementById('E-mail').style.background = "red";
        retorna = false;	
    }

    return retorna;
}


function checkdata() {
    returnadata = true;
    //alert("CHECKDATA");
    var expresio2 = new RegExp(/^((0?[1-9])|([12][0-9])|(3[01]))[-]((0?[1-9])|(1[0-2]))[-](19|20)\d{2}$/);
        data = document.getElementById('DataNaixament').value;
        if(expresio2.test(data)) {
            document.getElementById('labelData Naixament').innerHTML = "Data Naixament:";
            document.getElementById('DataNaixament').style.background = "AQUAMARINE";
        }
        else {
            miSpan=document.createElement("span");
            miSpan.setAttribute("style","color:red");
            miText = document.createTextNode(" * La data introduïda es incorrecte(DD-MM-YYYY)");
            miSpan.appendChild(miText);
            document.getElementById('labelData Naixament').innerHTML = "";
            document.getElementById('labelData Naixament').appendChild(miSpan);
            document.getElementById('DataNaixament').style.background = "red";
            document.getElementById('DataNaixament').focus();
            returnadata=false;
        }

    return returnadata;
}

function checkUserAjax(refForm) {
    
    var infoContacto = new Array();
    
    for(var i = 0; i<refForm.getElementsByTagName("input").length; i++) {
        if(refForm.getElementsByTagName("input")[i].name == "E-mail") infoContacto[0] = refForm.getElementsByTagName("input")[i].value;
        else if(refForm.getElementsByTagName("input")[i].name == "Password") infoContacto[1] = refForm.getElementsByTagName("input")[i].value;
    }
    
    //a verure si ho pillem be
    //alert("E-mail="+infoContacto[0]+"&Password="+infoContacto[1]);
    //Comienzo peticion Ajax
    var peticion_http = null;
    
    if(window.XMLHttpRequest) peticion_http = new XMLHttpRequest();
    else if(window.ActiveXObject) peticion_http = new ActiveXObject("Microsoft.XMLHTTP");
               
   document.getElementById("loading").innerHTML = "<img src='../../projecte-sintesi-dani-victor/site_media/img/ajax-loader.gif'>";
    
    peticion_http.onreadystatechange = getDisponibilitatContacto;

    peticion_http.open("POST", "../projecte-sintesi-dani-victor/site_media/php/usuari_check_exists.php", true);
    peticion_http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    peticion_http.send("E-mail="+infoContacto[0]+"&Password="+infoContacto[1]);
    
    function getDisponibilitatContacto() {
        if(peticion_http.readyState == 4) {
            if(peticion_http.status == 200) {
                if(peticion_http.responseText == "Usuario EXISTENTE") {
                    //alert("Usuario EXISTENTE");
                    document.getElementById("loading").innerHTML = "";
                    document.getElementById("loading").innerHTML = "Usuario EXISTENTE";
                    
                }
                else if(peticion_http.responseText == "Usuario NO EXISTENTE") {
                    document.getElementById("loading").innerHTML = "";
                    refForm.submit();
                }
            }
        }
    }  
}